<?php

class InvoicesController extends Controller{
  public $layout='//layouts/main';
  
  public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
  
  public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index', 'create', 'view', 'approve', 'decline'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
  
  public function actionApprove(){
    $id = $_GET['id'];
    $member = $_GET['member'];
    
    $invoice = Invoices::model()->find('id = :id',array(':id'=>$id));
    
    print_r($invoice->attributes);
    $invoice->status = Invoices::STATUS_APPROVED;
    $invoice->save();
    
    // extend the membership per year
    $user = Users::model()->find('id = :id', array(':id' => $invoice->user_id));
    
    if($invoice->membership_sought == 'National - 1 Year' || $invoice->membership_sought == 'International - 1 Year'){
      $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($user->expired_date)) . " +1 year"));
    }else{
      $date = date('Y-m-d', strtotime(date("Y-m-d", strtotime($user->expired_date)) . " +2 years"));
    }
    
    $user->expired_date = $date;
    $user->save();
    
    $this->redirect(bu("admin/members?member=".$member));
  }
  
  public function actionDecline(){
    $id = $_GET['id'];
    $member = $_GET['member'];
    $invoice = Invoices::model()->find('id = :id',array(':id'=>$id));
    $invoice->status = Invoices::STATUS_UNAPPROVED;
    $invoice->save();
    
    $this->redirect(bu("admin/members?member=".$member));
  }
  
  public function actionView($id){
  
  }
}